package com.alog.yzd.jdbc.hm;

import java.sql.*;

public class JdbcUtil3 {
    private static final String MYSQL_USERNAME = "root";
    private static final String MYSQL_PASSWORD = "123456";
    private static final String MYSQL_URL= "jdbc:mysql://localhost:3306/mydb";
    private static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";

    private JdbcUtil3(){}

    static {
        try {
            Class.forName(MYSQL_DRIVER);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static Connection getConnction() throws SQLException {
        return DriverManager.getConnection(MYSQL_URL, MYSQL_USERNAME, MYSQL_PASSWORD);
    }

    public static void release(ResultSet rs, Statement st, Connection conn){
        try {
            if(rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
